package tw.asap.plus.ui.activity;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import tw.asap.plus.R;
import tw.asap.plus.common.ConstValue;
import tw.asap.plus.entity.request.CommonRequestParams;
import tw.asap.plus.entity.response.ResponseDataWrapper;
import tw.asap.plus.entity.response.ShopIndex;
import tw.asap.plus.network.config.HRetrofitCreator;
import tw.asap.plus.network.service.UserService;
import tw.asap.plus.ui.AsapBaseActivity;
import tw.asap.plus.ui.adapter.MerchantListAdapter;
import tw.asap.plus.ui.custom.AppBar;
import tw.asap.plus.utils.Toaster;

/**
 * Created by Yax on 15/11/7.
 */
public class CollectionActivity extends AsapBaseActivity {


    @InjectView(R.id.appbar)
    AppBar appbar;
    @InjectView(R.id.recycler_view)
    RecyclerView recyclerView;
    @InjectView(R.id.swipe_refresh_layout)
    SwipeRefreshLayout swipeRefreshLayout;
    @InjectView(R.id.extra_layout)
    FrameLayout extraLayout;
    private MerchantListAdapter adapter;
    ArrayList<ShopIndex.ShopEntity> collectionList = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.layout_collection);
        ButterKnife.inject(this);
        if (swipeRefreshLayout != null) {
            swipeRefreshLayout.setRefreshing(false);
            swipeRefreshLayout.setColorSchemeResources(R.color.palette_red);
            swipeRefreshLayout.setOnRefreshListener(() ->
                    swipeRefreshLayout.postDelayed(() -> swipeRefreshLayout.setRefreshing(false), 1500l));
        }
        appbar.setLeftClickEvent(v -> CollectionActivity.this.finish());
        displayExtraLayout(0);
        getCollectionList();

    }

    private void getCollectionList() {
        HRetrofitCreator creator = HRetrofitCreator.getInstance();
        Observable.create(new Observable.OnSubscribe<ResponseDataWrapper<List<ShopIndex.ShopEntity>>>() {
            @Override
            public void call(Subscriber<? super ResponseDataWrapper<List<ShopIndex.ShopEntity>>> subscriber) {
                UserService service = creator.getService(UserService.class);
                subscriber.onNext(service.getMyCollection(new CommonRequestParams()));
                subscriber.onCompleted();
            }
        }).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io())
                .subscribe(new Subscriber<ResponseDataWrapper<List<ShopIndex.ShopEntity>>>() {
                    @Override
                    public void onCompleted() {
                        swipeRefreshLayout.setRefreshing(false);
                    }

                    @Override
                    public void onError(Throwable e) {
                        swipeRefreshLayout.setRefreshing(false);
                    }

                    @Override
                    public void onNext(ResponseDataWrapper<List<ShopIndex.ShopEntity>> shopList) {
                        if (shopList.respcd.equals(ConstValue.RESPCD)) {
                            if (shopList.data.size() == 0) {
                                displayExtraLayout(1);
                            } else {
                                if (adapter == null) {
                                    collectionList.addAll(shopList.data);
                                    initRecyclerView(collectionList);
                                } else {
                                    collectionList.clear();
                                    collectionList.addAll(shopList.data);
                                }
                                adapter.notifyDataSetChanged();
                            }
                        } else {
                            Toaster.s(CollectionActivity.this, shopList.resperr);
                        }
                    }
                });

    }

    private void initRecyclerView(List<ShopIndex.ShopEntity> list) {
        extraLayout.setVisibility(View.GONE);
        swipeRefreshLayout.setVisibility(View.VISIBLE);
        adapter = new MerchantListAdapter(this, list);
        LinearLayoutManager mLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setAdapter(adapter);
    }

    private void displayExtraLayout(int type) {
        if (extraLayout.getChildCount() > 0) {
            extraLayout.removeAllViews();
        }
        extraLayout.setVisibility(View.VISIBLE);
        switch (type) {
            case 0://loading
                swipeRefreshLayout.setVisibility(View.GONE);
                LayoutInflater.from(this).inflate(R.layout.layout_loading, extraLayout);
                break;
            case 1://noData
                swipeRefreshLayout.setVisibility(View.GONE);
                LayoutInflater.from(this).inflate(R.layout.layout_no_collection, extraLayout);
                break;
            case 2://noNetwork
                swipeRefreshLayout.setVisibility(View.GONE);
                LayoutInflater.from(this).inflate(R.layout.layout_nonetwork, extraLayout);
                extraLayout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        getCollectionList();
                    }
                });
                break;
        }
    }


    @Override
    public void onPause() {
        super.onPause();
        if (swipeRefreshLayout != null) {
            Log.d("onPause", "swipeRefreshLayout！＝null");
            swipeRefreshLayout.setRefreshing(false);
            swipeRefreshLayout.destroyDrawingCache();
            swipeRefreshLayout.clearAnimation();
        }
    }


}
